<!DOCTYPE html>
<html>
<head>
  <title>Test for SetDocumentTitleTransaction</title>
  <script type="text/javascript"
          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css"
          href="chrome://mochikit/content/tests/SimpleTest/test.css" />
</head>
<body onload="runTests()">
<div id="display">
  <iframe src="data:text/html,<!DOCTYPE html><html><head><title>first title</title></head><body></body></html>"></iframe>
</div>
<div id="content" style="display: none">
</div>
<pre id="test">
</pre>

<script class="testbody" type="application/javascript">
function runTests() {
  var iframe = document.getElementsByTagName("iframe")[0];
  function isDocumentTitleEquals(aDescription, aExpectedTitle) {
    is(iframe.contentDocument.title, aExpectedTitle, aDescription + ": document.title should be " + aExpectedTitle);
    is(iframe.contentDocument.getElementsByTagName("title")[0].textContent, aExpectedTitle, aDescription + ": The text in the title element should be " + aExpectedTitle);
  }

  isDocumentTitleEquals("Checking isDocumentTitleEquals()", "first title");

  const kTests = [
    { description: "designMode=\"on\"",
      init: function () {
        iframe.contentDocument.designMode = "on";
      },
      cleanUp: function () {
        iframe.contentDocument.designMode = "off";
      }
    },
    { description: "html element has contenteditable attribute",
      init: function () {
        iframe.contentDocument.documentElement.setAttribute("contenteditable", "true");
      },
      cleanUp: function () {
        iframe.contentDocument.documentElement.removeAttribute("contenteditable");
      }
    },
  ];

  for (var i = 0; i < kTests.length; i++) {
    const kTest = kTests[i];
    kTest.init();

    var editor = SpecialPowers.wrap(iframe.contentWindow).
                   QueryInterface(SpecialPowers.Ci.nsIInterfaceRequestor).
                   getInterface(SpecialPowers.Ci.nsIWebNavigation).
                   QueryInterface(SpecialPowers.Ci.nsIDocShell).editor;
    ok(editor, kTest.description + ": The docshell should have editor");
    var htmlEditor = editor.QueryInterface(SpecialPowers.Ci.nsIHTMLEditor);
    ok(htmlEditor, kTest.description + ": The editor should have nsIHTMLEditor interface");

    // Replace existing title.
    htmlEditor.setDocumentTitle("Modified title");
    isDocumentTitleEquals(kTest.description, "Modified title");

    // When the document doesn't have <title> element, title element should be created automatically.
    iframe.contentDocument.head.removeChild(iframe.contentDocument.getElementsByTagName("title")[0]);
    is(iframe.contentDocument.getElementsByTagName("title").length, 0, kTest.description + ": There should be no title element");
    htmlEditor.setDocumentTitle("new title");
    is(iframe.contentDocument.getElementsByTagName("title").length, 1, kTest.description + ": There should be a title element");
    isDocumentTitleEquals(kTest.description, "new title");

    kTest.cleanUp();
  }

  SimpleTest.finish();
}
</script>
</body>

</html>
